package com.zzh.ideas.web.service.impl;

import com.zzh.ideas.core.common.enums.FileType;
import com.zzh.ideas.core.common.utils.FileUtils;
import com.zzh.ideas.web.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * description： <br>
 * createTime: 2019/1/420:21 <br>
 *
 * @author zzh
 */
@Service
public class LocalFileServiceImpl implements FileService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${file.base.path}")
    public String basePath;

    @Override
    public boolean deleteFile(String path) throws Exception {
        return FileUtils.deleteFiles(basePath+ "/" + path);
    }


    @Override
    public boolean deleteDir(String path) throws Exception {
        return  FileUtils.deleteFiles(basePath+ "/" + path);
    }


    @Override
    public String uploadFile(MultipartFile file, String desPath, FileType fileType) throws Exception {
        String absolutePath = new File(basePath + "/" + desPath).getAbsolutePath();
        File toFile = new File(absolutePath + "/" + file.getOriginalFilename());
        FileUtils.createDir(toFile.getParentFile());
        file.transferTo(toFile);

        toFile.setLastModified(System.currentTimeMillis() - 2000L);
        return absolutePath + "/" + file.getOriginalFilename();
    }

    public String uploadFile1(MultipartFile file, String desPath, FileType fileType) throws Exception {
        String absolutePath = new File(basePath + "/" + desPath).getAbsolutePath();
        File toFile = new File(absolutePath + "/" + file.getOriginalFilename());
        FileUtils.createDir(toFile.getParentFile());
        try (FileOutputStream fos = new FileOutputStream(toFile);
             InputStream ins = file.getInputStream();) {
            byte[] buffer = new byte[4096];
            int read = 0;
            while ((read = ins.read(buffer)) != -1) {
                fos.write(buffer, 0, read);
                fos.flush();
            }
        }

        toFile.setLastModified(System.currentTimeMillis() - 2000L);
        return absolutePath + "/" + file.getOriginalFilename();
    }

    public String uploadFile2(MultipartFile file, String desPath, FileType fileType) throws Exception {
        String absolutePath = new File(basePath + "/" + desPath).getAbsolutePath();
        File toFile = new File(absolutePath + "/" + file.getOriginalFilename());
        FileUtils.createDir(toFile.getParentFile());
        FileOutputStream fos = null;
        InputStream ins = null;
        try {
            fos = new FileOutputStream(toFile);
            ins = file.getInputStream();
            byte[] buffer = new byte[4096];
            int read = 0;
            while ((read = ins.read(buffer)) != -1) {
                fos.write(buffer, 0, read);
                fos.flush();
            }
        } finally {
            if(fos != null) {
                try {
                    logger.info("上传文件，关闭文件流:" + toFile.getName());
                    fos.close();
                }catch (Exception e) {

                }
            }

            if(ins != null) {
                try {
                    ins.close();
                }catch (Exception e) {

                }
            }
        }

        toFile.setLastModified(System.currentTimeMillis() - 2000L);
        return absolutePath + "/" + file.getOriginalFilename();
    }


    @Override
    public boolean createDir(String path) throws Exception {
        return FileUtils.createDir(basePath+ "/" + path);
    }
}
